﻿using System;
using System.Diagnostics;
using System.Collections.Generic;
using System.Text;
using System.Threading;
using T3K.SketchEditor.Core;

namespace T3K.SketchEditor.Plugins {
    public class ExecPlugin : PluginBase {

        public ExecPlugin(): base("ExecPlugin") { }

        public override bool Init() {
            return true;
        }


        public void Command_ShellExecute(object sender, CommandArgs args) {

        }

        [Command("Execute", 
            Desc = "Executes an external command",
            ParamsDesc = "command as string, &arguments as string, &workingDir as string, &enviroment as Dictionary<string, string>")]
        public void Command_Execute(object sender, CommandArgs args) {


                string arguments = null;
                string startDir = null;
                Dictionary<string, string> enviroment = null;

                string command = args["Command"] as string;

                if (args.ContainsArg("Arguments", typeof(string)))
                    arguments = args["Arguments"] as string;

                if (args.ContainsArg("WorkingDir", typeof(string)))
                    startDir = args["WorkingDir"] as string;

                if (args.ContainsArg("EnviromentVars", typeof(Dictionary<string, string>)))
                    enviroment = args["EnviromentVars"] as Dictionary<string, string>;

                Process p = new Process();
                p.StartInfo.FileName = command;

                if (arguments != null)
                    p.StartInfo.Arguments = arguments;

                if (startDir != null)
                    p.StartInfo.WorkingDirectory = startDir;

                p.StartInfo.CreateNoWindow = true;
                p.StartInfo.RedirectStandardOutput = true;
                p.StartInfo.RedirectStandardError = true;
                p.StartInfo.UseShellExecute = false;
                p.StartInfo.LoadUserProfile = true;
                p.EnableRaisingEvents = true;
                p.OutputDataReceived += new DataReceivedEventHandler(p_OutputDataReceived);
                p.ErrorDataReceived += new DataReceivedEventHandler(p_OutputDataReceived);
                

                if (enviroment != null) {
                    foreach (string key in enviroment.Keys)
                        Environment.SetEnvironmentVariable(key, enviroment[key]);
                        //p.StartInfo.EnvironmentVariables[key] = enviroment[key];
                        //p.StartInfo.EnvironmentVariables.Add(key, enviroment[key]);
                }

                p.Start();
                p.BeginErrorReadLine();
                p.BeginOutputReadLine();
                p.WaitForExit(TimeSpan.FromSeconds(30).Milliseconds);

        }

        void p_OutputDataReceived(object sender, DataReceivedEventArgs e) {
            if (!string.IsNullOrEmpty(e.Data))
                Editor.InvokeCommand(this, "Main.Output.WriteLine", new CommandArgs("Out", e.Data));
        }

    }
}
